import modelExtend from 'dva-model-extend'
import { parse } from 'qs'
import { pageModel } from './common'
import { getAllMessage1,getNewMessage1,readOneMessage1,readAllMessage1 } from "services/message"

export default modelExtend(pageModel, {
    namespace: 'message',
    state: {
        list:"",
        pagination: {
			showSizeChanger: true,
			showQuickJumper: true,
			showTotal: total => `共 ${total} 条`,
			current: 1,
			total: null
        },
        list1:"",
        pagination1: {
			showSizeChanger: true,
			showQuickJumper: true,
			showTotal: total => `共 ${total} 条`,
			current: 1,
			total: null
		},
    },
    subscriptions: {
        setup({ dispatch, history }){
            history.listen(location => {
                if(location.pathname === '/sys/message') {
                    dispatch({
                        type:"getNewMessage"
                    })
                }
            })
        }
    },
    effects:{
        *reload(action, { put, select }){
            const page = yield select(({ message }) => message.pagination)
            yield put({
                type:"getNewMessage",
                payload:{
                    page: page.current,
                }
            })  
        },
        *getNewMessage({payload},{call,put}){
            const data = yield call(getNewMessage1,payload)
            if(data){
                yield put({
                    type: 'querySuccess1',
                    payload: {
                        list: data.page.list,
                        pagination: {
                            total: data.page.totalCount,
                            current: data.page.currPage
                        }
                    }
                })
            }
        },
        *getAllMessage({payload},{call,put}){
            const data = yield call(getAllMessage1,payload)
            if(data){
                yield put({
                    type: 'querySuccess2',
                    payload: {
                        list1: data.page.list,
                        pagination1: {
                            total: data.page.totalCount,
                            current: data.page.currPage
                        }
                    }
                })
            }
        },
        *readOneMessage({payload},{call,put}){
            const data = yield call(readOneMessage1,payload)
            yield put({ type: 'reload' })
        },
        *readAllMessage({},{call,put}){
            const data = yield call(readAllMessage1)
            yield put({ type: 'reload' })
        }
    },
    reducers:{
        querySuccess1(state, action) {
			const { list,pagination } = action.payload
			return {
				...state,
                list,
                pagination
			}
        },
        querySuccess2(state, action) {
			const { list1,pagination1 } = action.payload
			return {
				...state,
                list1,
                pagination1
			}
		},
    }
})